package com.algorithm.offer.linkedtable;


import java.util.ArrayList;
import java.util.Stack;

/**
 * @Author: huangzhigao
 * @Date: 2021/11/8 21:21
 * 输入一个链表的头节点，按链表从尾到头的顺序返回每个节点的值（用数组返回）
 */
public class JZ6 {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        if (null == listNode) {
            return new ArrayList<>();
        }

        Stack stack = new Stack();
        while (listNode != null) {
            stack.push(listNode.val);
            listNode = listNode.next;
        }
        ArrayList<Integer> res = new ArrayList<>();
        while (!stack.isEmpty()) {
            res.add((Integer) stack.pop());
        }
        return res;
    }
}

